package com.yuand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuand.entity.SysUserRole;
import com.yuand.entity.SysUser;
import com.yuand.mapper.SysUserRoleMapper;
import com.yuand.mapper.UserMapper;
import com.yuand.service.SysUserRoleService;
import com.yuand.service.UserService;
import com.yuand.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName: UserServiceImpl
 * @Author: yuandong
 * @Date 2022/1/18 10:13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserService {

    @Autowired
    private UserMapper  userMapper;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;


    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    @Override
    public SysUser selectByUsername(String username) {
        return this.userMapper.selectOne(new QueryWrapper<SysUser>().eq("username",username));
    }



    /**
     * 查询用户列表
     * @param user
     * @return
     */
    @Override
    public List<SysUser> selectUserList(SysUser user){
        return this.userMapper.selectUserList(user);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    public int insertUser(SysUser user){

        //新增用户
        user.setUserType("2");
        user.setStatus("0");
        user.setDelFlag(0);
        int rows = this.userMapper.insert(user);
        //新增用户和角色关联
        insertUserRole(user);
       return rows;
    }


    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    public SysUser selectById(Long id){
        SysUser user = this.userMapper.selectById(id);
        return user;
    }

    /**
     * 根据用户名查询用户(包括该用户的角色信息)
     * @param userId
     * @return
     */
    public SysUser selectUserById(Long userId){
        SysUser user = this.userMapper.selectUserById(userId);
        return user;
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    public int updateUserById(SysUser user){
        Long userId = user.getUserId();
        //1.先删除用户和角色的关联
        this.sysUserRoleMapper.deleteUserRoleByUserId(userId);
        //2:重新建立关联关系
        insertUserRole(user);
        //3:保存用户
        int rows = this.userMapper.updateById(user);
        return rows;
    }

    /**
     * 删除用户
     * @param userIds
     * @return
     */
    public int delUserByIds(Long[] userIds){
        //1:删除用户和角色的关联关系
        this.sysUserRoleMapper.deleteUserRole(userIds);
        //2：删除用户（批量）
        int rows = this.userMapper.deleteUserByIds(userIds);

       return rows;
    }

    /**
     * 用户分配角色
     * @param userId
     * @param roleIds
     */
    @Override
    public void updateUserRole(Long userId, Long[] roleIds) {
        //1:根据userId删除sys_user_role的关联关系
        this.sysUserRoleMapper.deleteUserRoleByUserId(userId);
        //2:重新建立用户和角色的关联关系
        SysUser user=new SysUser();
        user.setUserId(userId);
        user.setRoleIds(roleIds);
        this.insertUserRole(user);
    }

    /**
     * 角色列表页面--点击某一个角色重新分页用户
     * @param user
     * @return
     */
    @Override
    public List<SysUser> selectAllocatedUserList(SysUser user) {

        List<SysUser> sysUsers = this.userMapper.selectAllocatedUserList(user);

        return sysUsers;
    }


    /**
     * 查询角色未授权用户列表
     * @param user
     * @return
     */
    public List<SysUser> selectUnAllocatedUserList(SysUser user){
        List<SysUser> sysUsers =this.userMapper.selectUnAllocatedUserList(user);
        return sysUsers;
    }


    /**
     * 批量选择用户授权
     * @param userIds
     * @param roleId
     * @return
     */
    public void saveUserRole(Long[] userIds, Long roleId){
       List<SysUserRole> userRoles = new ArrayList<>();
        for(Long userId:userIds){
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoles.add(userRole);
        }
        this.sysUserRoleService.saveBatch(userRoles);
    }


    /**
     * 批量保存用户和角色
     * @param user
     */
    public void insertUserRole(SysUser user){
        Long[] roldIds = user.getRoleIds();
        List<SysUserRole> userRoleList=new ArrayList<>();
        if(!Objects.isNull(roldIds)){
            for(Long roleId:roldIds){
                SysUserRole userRole = new SysUserRole();
                userRole.setRoleId(roleId);
                userRole.setUserId(user.getUserId());
                userRoleList.add(userRole);
            }
        }

        if(userRoleList.size()>0){
            //mybatisplus中的mapper接口只提供基本的增删改操作，service接口又继续扩展了批量的操作方法
            this.sysUserRoleService.saveBatch(userRoleList);
        }

    }
}
